package com.hrm.company.controller;

import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import com.hrm.company.service.CompanyService;
import com.hrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    /**
     * 保存企业
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result addCompany(@RequestBody Company company) {
        companyService.addCompany(company);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id更新企业
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result updateCompany(@PathVariable(value = "id") String id, @RequestBody Company company) {
        company.setId(id);
        companyService.updateCompany(company);
        return new Result(ResultCode.SUCCESS);
    }
    /**
     * 根据id删除企业
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result deleteCompany(@PathVariable(value = "id") String id){
        companyService.deleteCompany(id);
        return new Result(ResultCode.SUCCESS);
    }
    /**
     * 根据id查询企业
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findCompanyById(@PathVariable(value = "id") String id){
        Company company = companyService.findCompanyById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(company);
        return result;
    }
    /**
     * 查询全部企业列表
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public Result findCompanies(){
        List<Company> companies = companyService.findCompanies();
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(companies);
        return result;
    }
}
